const { transactionUpdate } = require("../../../../db");
const { TokenStatus, MaxRequestCount } = require("../../../../constEnum");
const { handleError } = require("./tokenError");
const { changeToLoading } = require("../changeToLoading");
async function handleInvalid(doc) {
    let { requestCount = 0 } = doc;
    if (requestCount > MaxRequestCount) {
        if (doc.tokenStatus !== TokenStatus.error) {
            // 开启事务,  更新 token状态 为error
            let update = {
                requestCount: 0,
                tokenStatus: TokenStatus.error,
            };
            await transactionUpdate(doc._id, update);
        }
        // 返回error
        return handleError();
    }
    // 更新数据库 ,  然后去请求
    return await changeToLoading(doc);
}

module.exports = {
    handleInvalid,
};
